package com.bwton.msx.ccb.ui.auth;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.bwton.msx.ccb.databinding.FragmentFaceCancelBinding;
import com.bwton.msx.ccb.databinding.FragmentLoginBinding;
import com.bwton.msx.ccb.service.api.ApiServiceManager;
import com.bwton.msx.ccb.service.api.annotations.FaceOperate;
import com.bwton.msx.ccb.service.api.response.LoginResult;
import com.bwton.msx.ccb.service.auth.AuthService;
import com.bwton.msx.ccb.service.user.User;
import com.bwton.msx.ccb.service.user.UserManager;
import com.bwton.msx.ccb.utils.RxViewUtils;

import java.util.Objects;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class FaceCancelFragment extends Fragment {
    private CompositeDisposable disposable = new CompositeDisposable();
    private FragmentFaceCancelBinding binding;
    private AuthService authService = new AuthService(ApiServiceManager.getAuth());
    private final String TAG = "登录";
    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        binding = FragmentFaceCancelBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        disposable = new CompositeDisposable();
        //冻结
        Disposable freeze = RxViewUtils.throttle(binding.btnFreeze, 10).subscribe(v -> {
            operateFace("1");
        });
        disposable.add(freeze);
        //解冻
        Disposable thaw = RxViewUtils.throttle(binding.btnThaw, 10).subscribe(v -> {
            operateFace("2");
        });
        disposable.add(thaw);
        //关闭
        Disposable close = RxViewUtils.throttle(binding.btnClose, 10).subscribe(v -> {
            operateFace("3");
        });
        disposable.add(close);
    }
    private void operateFace(@FaceOperate String action){
        Disposable operate = authService.faceOperate(action)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(result->{
                    binding.textRequest.setText(result.toString());
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                });
        disposable.add(operate);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
        disposable.dispose();
        disposable.clear();
    }

}